package com.wx.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wx.domain.Transaction;
import com.wx.service.PatientService;
import com.wx.service.TransactionService;
import com.wx.service.impl.PatientServiceImpl;
import com.wx.service.impl.TransactionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/transactionByPatient")
public class TransactionByPatient extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");

        String patientId = req.getParameter("patientId");
        TransactionService transactionService=new TransactionServiceImpl();
        PatientService patientService=new PatientServiceImpl();
        List<Transaction> transactionList=transactionService.findByPatientId(patientId);
        double wallet=patientService.getWalletById(patientId);
        ObjectMapper mapper=new ObjectMapper();
        Map<String, Object> map=new HashMap<>();
        map.put("transactionList",transactionList);
        map.put("wallet",wallet);
        mapper.writeValue(resp.getWriter(),map);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
